const fs = require('fs')
const inquirer = require('inquirer')
const utils = require('./utils')
const plist = require('plist')
const symbols = require('log-symbols')

// plist 文件的默认值
var defaultExport = {
    'compileBitcode': false,
    'destination': 'export',
    'stripSwiftSymbols': true,
    'thinning': '<none>'
}

/**
 * 初始化一个 ExportOptions.plist 
 * 
 * 这个文件在最终的打包产物中可以找到，名称就是：ExportOptions.plist，需要提前配置好，xcode 打包的话就是 xcode 自动配置的
 * 
 * - 终端输入 + 默认值 = .plist
 */
async function init() {
    console.log('This utility will walk you through creating a ExportOptions.plist')
    console.log('* 打包时必须有的文件')
    console.log('* 创建一次之后，后续打包使用默认值')
    console.log('* 创建之后可手动进行修改')
    console.log('* 打包之前，请确认该文件已正确填写')
    console.log(' ')

    let answer = await initParams()
    createExportOptionsPlist(answer)
}

// 自定义终端输入
function initParams() {
    const promptList = [
        {
            type: 'list',
            message: '选择打包方式: ',
            name: 'method',
            choices: [
                'development',
                'adhoc',
                'enterprise',
                'app-store'
            ]
        },
        {
            type: 'list',
            message: '选择签名方式: ',
            name: 'signingStyle',
            choices: [
                'manual',
                'automatic'
            ]
        },
        {
            type: 'list',
            message: '选择 signing Certificate: ',
            name: 'signingCertificate',
            choices: [
                'Apple Developer',
                'Apple Distribution'
            ]
        },
        {
            type: 'input',
            message: '请填写 Bundle Identifier: ',
            name: 'bundle_identifer',
            when: function(answers) {
                return answers.signingStyle == 'manual'
            }
        },
        {
            type: 'input',
            message: '请填写 Provisioning Profile: ',
            name: 'provisioning_profile',
            when: function(answers) {
                return answers.signingStyle == 'manual'
            }
        },
        {
            type: 'input',
            message: '请填写 Notification Bundle Identifier: ',
            name: 'noti_bundle_identifer',
            when: function(answers) {
                return answers.signingStyle == 'manual'
            }
        },
        {
            type: 'input',
            message: '请填写 Notification Provisioning Profile: ',
            name: 'noti_provisioning_profile',
            when: function(answers) {
                return answers.signingStyle == 'manual'
            }
        },
        {
            type: 'input',
            message: '请填写 teamID: ',
            name: 'team_id',
            when: function(answers) {
                return answers.signingStyle == 'manual'
            }
        }
    ]
    return new Promise((resolve, reject) => {
        inquirer.prompt(promptList)
            .then(answer => {
                resolve(answer)
            })
    })
}

/*
 * 创建 ExportOptions.plist 
 *
 * - 用户输入内容 + defaultExport 拼凑成固定格式
 * - 上一步内容转化为 xml 写入 plist
 */
function createExportOptionsPlist(answer) {
    var result = {}
    result.method = answer.method
    result.signingStyle = answer.signingStyle
    result.signingCertificate = answer.signingCertificate
    if (answer.signingStyle == 'manual') {
        let provisioningProfiles = {}
        provisioningProfiles[`${answer.bundle_identifer}`] = answer.provisioning_profile
        provisioningProfiles[`${answer.noti_bundle_identifer}`] = answer.noti_provisioning_profile
        result.provisioningProfiles = provisioningProfiles
        result.teamID = answer.team_id
    }
    let merge = Object.assign(defaultExport, result)
    let xml = plist.build(merge)

    fs.writeFile(utils.exportPlistPath, xml, err => {
        if (err) {
            console.log(err)
            process.exit(1)
        }
        console.log(' ')
        console.log(symbols.success, `${utils.exportPlistName} 文件已生成`)
    })
}

module.exports = init;